package com.leetcode;

/**
 * 209. 长度最小的子数组
 * 滑动窗口
 */
public class Solution209_2 {

    public int minSubArrayLen(int target, int[] nums) {
        int n = nums.length;
        int start = 0;
        int end = 0;
        int length = n + 1;
        int sum = 0;
        while (end < n) {
            sum += nums[end];
            while (sum >= target) {
                length = Math.min(length, end - start + 1);
                sum -= nums[start];
                start++;
            }
            end++;
        }
        return length == n + 1 ? 0 : length;
    }

    public static void main(String[] args) {
        int nums[] = new int[]{2, 3, 1, 2, 4, 3};
        int target = 7;
        int minLength = new Solution209_2().minSubArrayLen(target, nums);
        System.out.println(minLength);
    }
}
